﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    using DAL.Entities;

    public class NhanVienDAL : BaseDAL, UTL.IBaseDAL
    {
        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            string sql = @" SELECT * ,b.ten_cv as ten_cv,c.ten_pb as ten_pb FROM nhanvien a join chucvu b on a.ma_cv = b.ma_cv join phongban c on a.ma_pb = c.ma_pb ";
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            string sql = @" DELETE FROM nhanvien WHERE ma_nv = '{0}' ";
            sql = string.Format(sql,id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (Nhan_Vien)obj;
            string sql = @"set dateformat mdy; INSERT INTO nhanvien(ma_nv,ten_nv,ngaysinh,cmnd,sdt,diachi,ma_cv,ma_pb,taikhoan,matkhau) VALUES('{0}',N'{1}','{2}','{3}','{4}',N'{5}','{6}','{7}')";
            sql = string.Format(sql,o.ma_nv,o.ten_nv,o.namsinh_nv,o.cmnd_nv,o.sdt_nv,o.diachi_nv,o.macv_nv,o.mapb_nv);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Nhan_Vien)obj;
            string sql = @"set dateformat mdy; UPDATE nhanvien SET ten_nv=N'{0}', ngaysinh='{1}', cmnd='{2}', sdt='{3}', diachi=N'{4}', ma_cv='{5}', ma_pb='{6}' WHERE ma_nv='{7}' ";
            sql = string.Format(sql, o.ten_nv, o.namsinh_nv, o.cmnd_nv, o.sdt_nv, o.diachi_nv, o.macv_nv, o.mapb_nv,o.ma_nv);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            throw new NotImplementedException();
        }

        public DataTable Select_chucvu()
        {
            string sql = @" SELECT ma_cv as id, ten_cv FROM chucvu ";
            return ExecuteQuery(sql);
        }
        public DataTable Select_phongban()
        {
            string sql = @" SELECT ma_pb as id, ten_pb FROM phongban ";
            return ExecuteQuery(sql);
        }

    }
}
